package uk.ac.manchester.tewpl.iceiface.client.lists;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

import uk.ac.manchester.tewpl.iceiface.client.Functors.ParamCallback;
import uk.ac.manchester.tewpl.iceiface.client.Functors.ParamVoidCallback;
import uk.ac.manchester.tewpl.iceiface.client.Functors.TypedCallback;
import uk.ac.manchester.tewpl.iceiface.client.Functors.VoidCallback;
import uk.ac.manchester.tewpl.iceiface.client.model.KBClassModel;
import uk.ac.manchester.tewpl.iceiface.client.model.KBIndividualModel;
import uk.ac.manchester.tewpl.iceiface.client.model.KBWorkspaceModel;
import uk.ac.manchester.tewpl.iceiface.client.service.KBServiceAsync;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.store.ListStore;

import com.extjs.gxt.ui.client.widget.CheckBoxListView;
import com.extjs.gxt.ui.client.widget.Composite;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class TagList extends ContentPanel {
	
	private KBClassModel tagClass;
	private ArrayList<KBIndividualModel> tags;
	private CheckBoxListView<KBIndividualModel> listView;
	
	public TagList(KBClassModel cls, Collection<KBIndividualModel> indivs) {
		// Invoke super constructor
		super();
		
		// Store class and tags
		tagClass = cls;
		tags = new ArrayList<KBIndividualModel>(indivs);
		
		// Store tags
		Collections.sort(tags, new Comparator<KBIndividualModel>() {
			@Override
			public int compare(KBIndividualModel indiv1, KBIndividualModel indiv2) {
				return indiv1.getLabel().compareTo(indiv2.getLabel());
			}
		});
		
		// Create container
		this.setHeaderVisible(true);
		this.setHeading(cls.getLabel());
		
		// Create and populate list		
		ListStore<KBIndividualModel> ls = new ListStore<KBIndividualModel>();
		ls.add(new ArrayList<KBIndividualModel>(tags));
		
		listView = new CheckBoxListView<KBIndividualModel>();
		listView.setStore(ls);
		listView.setBorders(false);
		listView.setDisplayProperty("label");

		this.add(listView);
	}
	
	/**
	 * Factory to create a TagList with the given KBClassModel,
	 * uses a callback on completion
	 * 
	 * @param KBClassModel
	 * @param ParamVoidCallback<TagList>
	 */
	public static void create(final KBClassModel cls, final ParamVoidCallback<TagList> cb) {
		// Get individuals from class
		KBServiceAsync kbservice = Registry.get("kbservice");
		KBWorkspaceModel ws = Registry.get("workspace");
		
		kbservice.findIndividuals(cls, ws, new AsyncCallback<Collection<KBIndividualModel>>() {
			
			@Override
			public void onSuccess(Collection<KBIndividualModel> indivs) {
				cb.work(new TagList(cls, indivs));
			}
			
			@Override
			public void onFailure(Throwable arg0) {
				cb.work(null);
			}
			
		});
		
	}
	
	/**
	 * Return's the position tag class' label
	 * @return
	 */
	public String getLabel() {
		return tagClass.getLabel();
	}
	
	public void addSelectionChangedListener(SelectionChangedListener<KBIndividualModel> sl) {
		listView.getSelectionModel().addSelectionChangedListener(sl);
	}
	
	public Collection<KBIndividualModel> getCheckedTags() {
		return listView.getChecked();
	}
}
